﻿Imports DTO
Imports BUS
Public Class frmLuuMachDieuKhien
    Private _trangThaiDangNhap As Boolean
    Public Sub TrangThai(trangthai As Boolean)
        _trangThaiDangNhap = trangthai
    End Sub
    
    Public Sub addList(ByVal ids As List(Of Integer))
        Dim dp As New MachDieuKhienBUS
        Dim ds As List(Of MachDieuKhienDTO) = dp.layDSMachDKs(ids)
        For Each obj As MachDieuKhienDTO In ds
            Dim TenLoai As String = (New LoaiMachDieuKhienBUS).layDSLoaiMDK().Item(obj.MaLoai - 1).TenLoai
            Dim row As String() = New String() {obj.MaMach, obj.TenMach, obj.HangSanXuat, TenLoai, obj.MaLoai, obj.DongDien, obj.HieuSuatTangThem, obj.SoCongPin, obj.Gia}
            dgvDuLieuThem.Rows.Add(row)
        Next

    End Sub

    Private Sub frmLuuMachDieuKhien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mdiparent As Main = DirectCast(Me.MdiParent, Main)
        If mdiparent.DaDangNhap = False Then
            If MessageBox.Show("Bạn chưa nhập Mật Khẩu để thực hiện chỉnh sửa Cơ Sở Dữ Liệu. Bạn có muốn nhập Mật Khẩu không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                Dim frmMatKhau As New NhapMatKhau()
                'frmMatKhau.MdiParent = Me.MdiParent
                Dim result = frmMatKhau.ShowDialog()
                If result = Windows.Forms.DialogResult.Cancel Then
                    Me.Close()
                ElseIf result = Windows.Forms.DialogResult.OK Then

                    mdiparent.DangNhap()
                End If
            Else
                Me.Close()
            End If
        End If
        'btnUpdate.Enabled = False
        Dim dp As New LoaiMachDieuKhienBUS
        'cbLoaiMach.DataSource = dp.layDSLoaiMDK()
        With cbLoaiMach
            .DataSource = dp.layDSLoaiMDK()
            .DisplayMember = "TenLoai"
            .ValueMember = "MaLoai"
            .SelectedIndex = 0
        End With
        btnInput.Enabled = False
        txtDiaChiFileExcel.Enabled = False

        'Dim dp2 As New MachDieuKhienBUS
        ''dgvDuLieuThem.DataSource = dp2.layDSMachDK()

        'For Each obj As MachDieuKhienDTO In dp2.layDSMachDK()
        '    Dim TenLoai As String = dp.layDSLoaiMDK().Item(obj.MaLoai - 1).TenLoai
        '    Dim row As String() = New String() {obj.MaMach, obj.TenMach, obj.HangSanXuat, TenLoai, obj.MaLoai, obj.DongDien, obj.HieuSuatTangThem, obj.SoCongPin, obj.Gia}
        '    dgvDuLieuThem.Rows.Add(row)
        'Next

    End Sub

    Private Sub cbInputFromFile_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbInputFromFile.CheckedChanged
        Dim chkbox As CheckBox = DirectCast(sender, CheckBox)
        If Not chkbox.ThreeState Then
            chkbox.ThreeState = True
            btnInput.Enabled = True
            txtDiaChiFileExcel.Enabled = True
        Else
            chkbox.ThreeState = False
            btnInput.Enabled = False
            txtDiaChiFileExcel.Enabled = False
        End If
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim _tenMach = txtTenMDK.Text
        Dim _maLoai As Integer = cbLoaiMach.SelectedValue
        Dim _tenLoai As String = cbLoaiMach.Text
        Dim _hangSX = txtHangSX.Text
        Dim _soCongPin As Integer = txtSoCongPin.Text
        Dim _dongDien As Double = txtDongDien.Text
        Dim _HSTangThem As Double = txtHSTangThem.Text
        Dim _gia As Integer = txtGia.Text

        'Dim dp As New BUS.MachDieuKhienBUS
        'Dim rs As Integer = dp.themMachDK(_tenMach, _maLoai, _hangSX, _soCongPin, _dongDien, _HSTangThem, _gia)

        'If (rs = 1) Then
        '    MessageBox.Show("Them thanh cong")
        '    Dim _maMach = dp.layMaMachLonNhat()
        Dim row As String() = New String() {0, _tenMach, _hangSX, _tenLoai, _maLoai, _dongDien, _HSTangThem, _soCongPin, _gia}
        dgvDuLieuThem.Rows.Add(row)

        'End If

    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Close()
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim dp As New MachDieuKhienBUS
        Dim _maMach = dgvDuLieuThem.CurrentRow.Cells("MaMach").Value.ToString()
        Dim _tenMach = txtTenMDK.Text
        Dim _maLoai As Integer = cbLoaiMach.SelectedValue
        Dim _tenLoai As String = cbLoaiMach.Text
        Dim _hangSX = txtHangSX.Text
        Dim _soCongPin As Integer = txtSoCongPin.Text
        Dim _dongDien As Integer = txtDongDien.Text
        Dim _HSTangThem As Integer = txtHSTangThem.Text
        Dim _gia As Integer = txtGia.Text

        'Dim rs As Integer = dp.capNhatMachDK(_maMach, _tenMach, _maLoai, _hangSX, _soCongPin, _HSTangThem, _dongDien, _gia)
        'If (rs = 1) Then
        '    MessageBox.Show("Cap nhat thanh cong")
        For Each row As DataGridViewRow In dgvDuLieuThem.SelectedRows
            row.Cells("TenMach").Value = txtTenMDK.Text
            row.Cells("DongDien").Value = txtDongDien.Text
            row.Cells("Gia").Value = txtGia.Text
            row.Cells("HangSanXuat").Value = txtHangSX.Text
            row.Cells("HieuSuatTangThem").Value = txtHSTangThem.Text
            row.Cells("SoCongPin").Value = txtSoCongPin.Text
            row.Cells("TenLoai").Value = cbLoaiMach.Text
        Next
        'Dim row As DataGridViewRow = dgvDuLieuThem.CurrentRow
        'row.Cells("TenMach").Value = txtTenMDK.Text
        'row.Cells("DongDien").Value = txtDongDien.Text
        'row.Cells("Gia").Value = txtGia.Text
        'row.Cells("HangSanXuat").Value = txtHangSX.Text
        'row.Cells("HieuSuatTangThem").Value = txtHSTangThem.Text
        'row.Cells("SoCongPin").Value = txtSoCongPin.Text
        'row.Cells("TenLoai").Value = cbLoaiMach.Text
        'End If
    End Sub

    Private Sub dgvDuLieuThem_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDuLieuThem.CellContentClick
        'MessageBox.Show("hello")
    End Sub

    Private Sub dgvDuLieuThem_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDuLieuThem.SelectionChanged
        'MessageBox.Show("hello")
    End Sub

    Private Sub dgvDuLieuThem_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDuLieuThem.CellClick
        'MessageBox.Show("hello")
        'Dim i, j As Integer
        'i = dgvDuLieuThem.CurrentRow.Index
        Dim row As DataGridViewRow = dgvDuLieuThem.CurrentRow
        txtTenMDK.Text = row.Cells("TenMach").Value.ToString()
        txtDongDien.Text = row.Cells("DongDien").Value.ToString()
        txtHangSX.Text = row.Cells("HangSanXuat").Value.ToString()
        txtSoCongPin.Text = row.Cells("SoCongPin").Value.ToString()
        txtHSTangThem.Text = row.Cells("HieuSuatTangThem").Value.ToString()
        txtGia.Text = row.Cells("Gia").Value.ToString()
        cbLoaiMach.SelectedIndex = Integer.Parse(row.Cells("MaLoai").Value.ToString()) - 1

        'btnUpdate.Enabled = True
        'btnThem.Enabled = False
        'btnNhapLai.Enabled = False

    End Sub

    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapLai.Click
        txtDongDien.Text = ""
        txtGia.Text = ""
        txtHangSX.Text = ""
        txtHSTangThem.Text = ""
        txtSoCongPin.Text = ""
        txtTenMDK.Text = ""
        cbLoaiMach.SelectedIndex = 0

    End Sub

    Private Sub btnXoaMot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaMot.Click
        If (dgvDuLieuThem.SelectedRows.Count > 0) Then

            'Dim dp As New MachDieuKhienBUS
            'Dim maMach As Integer = Integer.Parse(dgvDuLieuThem.CurrentRow.Cells("MaMach").Value.ToString())
            'Dim ret As Integer = dp.xoaMachDK(maMach)
            'If (ret = 1) Then
            MessageBox.Show("Xoa thanh cong")
            dgvDuLieuThem.Rows.Remove(dgvDuLieuThem.CurrentRow)

            'End If
        End If
    End Sub

    Private Sub btnXoaHet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaHet.Click
        dgvDuLieuThem.Rows.Clear()
    End Sub

    Private Sub btnLuuTatCa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuuTatCa.Click
        Try
            For Each row As DataGridViewRow In dgvDuLieuThem.Rows
                Dim _maMach = Integer.Parse(row.Cells("MaMach").Value.ToString())
                Dim _tenMach = row.Cells("TenMach").Value.ToString()
                Dim _maLoai As Integer = Integer.Parse(row.Cells("MaLoai").Value.ToString())

                Dim _hangSX = row.Cells("HangSanXuat").Value.ToString()
                Dim _soCongPin As Integer = row.Cells("SoCongPin").Value.ToString()
                Dim _dongDien As Integer = row.Cells("DongDien").Value.ToString()
                Dim _HSTangThem As Integer = row.Cells("HieuSuatTangThem").Value.ToString()
                Dim _gia As Integer = row.Cells("Gia").Value.ToString()

                Dim dp As New BUS.MachDieuKhienBUS
                Dim rs As Integer = dp.capNhatMachDK(_maMach, _tenMach, _maLoai, _hangSX, _soCongPin, _HSTangThem, _dongDien, _gia)
                If (rs = 0) Then
                    rs = dp.themMachDK(_tenMach, _maLoai, _hangSX, _soCongPin, _dongDien, _HSTangThem, _gia)
                End If

            Next
            MessageBox.Show("Luu thanh cong")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try


    End Sub
End Class